package sc.love.boot.es;

import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class EsConfig {
    private static String IP;
    private static int PORT;

    public void init(){
        this.IP = "192.168.12.70";
        this.PORT = 9200;
    }

    public static void esClient(){
        try {
            Settings settings = Settings.builder().put("cluster.name", "elasticsearch-cluster").build();
            TransportClient client = new PreBuiltTransportClient(settings)
                    .addTransportAddresses(new TransportAddress(InetAddress.getByName("192.168.12.70"), 9300));
            try {
                IndexResponse response = client.prepareIndex("hlog-crm3-hig-c-consum-3", "hlog").setSource(XContentFactory.jsonBuilder()
                        .startObject().field("ip", "128.0.0.1")
                        .field("method", "PUT")
                        .endObject()).get();
                System.out.println("索引名称:" + response.getIndex() + "\n类型:" + response.getType()
                        + "\n文档ID:" + response.getId() + "\n当前实例状态:" + response.status());
            } catch (IOException e) {
                e.printStackTrace();
            }


        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        esClient();
    }


}
